package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;
    @RequestMapping("/add")
    public Result add(int[] checkitemIds, @RequestBody CheckGroup checkGroup){
        try {
            Result add = checkGroupService.add(checkitemIds, checkGroup);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

        return  new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);


    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = checkGroupService.findPage(queryPageBean);
        return  page;
    }
    @RequestMapping("/delete")
    public Result delete(int id){
        try {

            Result page = checkGroupService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);


    }
    @RequestMapping("/findAndUp")
    public Result findItemIdsByGroupId(int id){
        Result page = checkGroupService.findItemIdsByGroupId(id);
        return  page;
    }
    @RequestMapping("/edit")
    public Result edit(int[] checkitemIds,@RequestBody CheckGroup checkGroup){

        try {
            Result page = checkGroupService.edit(checkitemIds,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);

    }
    @RequestMapping("/findAll")
    public Result findAll(){
        return checkGroupService.findAll();
    }

}
